package com.example.wlzz.controller;

import com.example.wlzz.constant.RedisConstant;
import com.example.wlzz.dto.UserInfoDTO;
import com.example.wlzz.entity.UserInfo;
import com.example.wlzz.repository.mapper.UserInfoMapper;
import com.example.wlzz.repository.mongoRepository.KlassRepository;
import com.example.wlzz.service.UserInfoService;
import com.example.wlzz.utils.ResultVOUtil;
import com.example.wlzz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/getInfo")
@Slf4j
@Api(tags = "用户获取信息模块")
@CrossOrigin
public class UserGetInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private KlassRepository klassRepository;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @PostMapping("/getUserInfo")
    @ApiOperation("用户获取个人信息")
    public ResultVO alterNickName(@ApiParam("token凭据") @RequestParam("token") String token){
        return  ResultVOUtil.success(userInfoService.getUserInfo(token));
    }
    @PostMapping("/getKlass")
    @ApiOperation("用户获取当前学期理论课表")
    public ResultVO getKlass(@ApiParam("token凭据") @RequestParam("token") String token){
        //去redis查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        return ResultVOUtil.success(klassRepository.findBy_id(tokenValue));
    }
    @PostMapping("/useridGetForInfo")
    @ApiOperation("根据userid获取用户的公开信息")
    public ResultVO useridGetForInfo(@ApiParam("token凭据") @RequestParam("token") String token,
                                     @ApiParam("其他用户的userid") @RequestParam("userid") int userid){
        return userInfoService.useridGetForInfo(userid);
    }
    @PostMapping("/getRoll")
    @ApiOperation("获取信用值")
    public ResultVO GetRoll(@ApiParam("token凭据") @RequestParam("token") String token){
        //去redis查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.LOGIN_SUCCESS_TOKEN, token));
        String rollNmber =  userInfoMapper.getRollNumber(tokenValue);
        Map<String,String> map = new HashMap<>();
        map.put("rollNumber",rollNmber);
        return ResultVOUtil.success(map);
    }
}
